package com.iespring.service.poromotion;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iespring.bean.admin.promotion.bo.MallAdBo;
import com.iespring.bean.admin.promotion.bo.MallAdForUpdateBo;
import com.iespring.bean.admin.promotion.po.MallAd;
import com.iespring.bean.admin.promotion.po.MallAdExample;
import com.iespring.bean.admin.promotion.vo.MallAdVo;
import com.iespring.bean.common.BasePageInfo;
import com.iespring.bean.common.BaseRespVo;
import com.iespring.bean.common.CommonData;
import com.iespring.mapper.admin.MallAdMapper;
import com.iespring.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author:ZhangShuPeng
 * @Date:2022/12/28/21:51
 * @Description:
 */
@Service
public class PromotionAdServiceImpl implements PromotionAdService {

    @Autowired
    MallAdMapper mallAdMapper;


    /**
     * @param info    分页：page/limit    排序：sort/order
     * @param name    广告标题
     * @param content 广告内容
     * @return CommonData
     * page=1&limit=20&name=99&content=%E4%BD%A0%E5%A5%BD&sort=add_time&order=desc
     */
    @Override
    public CommonData<MallAd> list(BasePageInfo info, String name, String content) {

        MallAdExample mallAdExample = new MallAdExample();

        //获得分页信息
        PageHelper.startPage(info.getPage(), info.getLimit());

        //排序
        mallAdExample.setOrderByClause(info.getSort() + " " + info.getOrder());
        MallAdExample.Criteria criteria = mallAdExample.createCriteria();

        //逻辑删除属性deleted为false时才可以显示，为true时不显示
        criteria.andDeletedEqualTo(false);

        //如果name(广告标题)和content(广告内容)不为空，对其拼接进行模糊查询
        if (!StrUtil.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (!StrUtil.isEmpty(content)) {
            criteria.andContentLike("%" + content + "%");
        }
        List<MallAd> mallAds = mallAdMapper.selectByExample(mallAdExample);

        //对结果进行封装
        PageInfo<MallAd> mallAdPageInfo = new PageInfo<>(mallAds);
//        int pageNum = mallAdPageInfo.getPageNum();
//        int pages = mallAdPageInfo.getPages();
//        int pageSize = mallAdPageInfo.getPageSize();
//        long total = mallAdPageInfo.getTotal();
//        List<MallAd> list = mallAdPageInfo.getList();
//        CommonData data = new CommonData((int) total, pages, pageSize, pageNum, list);

        return CommonData.data(mallAdPageInfo);
    }

    @Override
    public BaseRespVo create(MallAdBo mallAdBo) {

        String content = mallAdBo.getContent();
        String link = mallAdBo.getLink();
        String name = mallAdBo.getName();
        Byte position = mallAdBo.getPosition();
        Boolean enabled = mallAdBo.getEnabled();
        String url = mallAdBo.getUrl();

        MallAd mallAd = new MallAd();


        mallAd.setName(name);
        mallAd.setContent(content);
        mallAd.setUrl(url);
        mallAd.setLink(link);
        mallAd.setPosition(position);
        mallAd.setEnabled(enabled);
        mallAd.setAddTime(new Date());
        mallAd.setUpdateTime(new Date());

        mallAdMapper.insertSelective(mallAd);

        MallAdVo mallAdVo = new MallAdVo();

        mallAdVo.setId(mallAd.getId());
        mallAdVo.setUrl(mallAd.getUrl());
        mallAdVo.setAddTime(mallAd.getAddTime());
        mallAdVo.setContent(mallAd.getContent());
        mallAdVo.setEnabled(mallAd.getEnabled());
        mallAdVo.setLink(mallAd.getLink());
        mallAdVo.setName(mallAd.getName());
        mallAdVo.setPosition(mallAd.getPosition());
        mallAdVo.setUpdateTime(mallAd.getUpdateTime());


        return BaseRespVo.ok(mallAdVo);
    }

    @Override
    public BaseRespVo update(MallAdForUpdateBo mallAdForUpdateBo) {
        String content = mallAdForUpdateBo.getContent();
        String link = mallAdForUpdateBo.getLink();
        String name = mallAdForUpdateBo.getName();
        Byte position = mallAdForUpdateBo.getPosition();
        Boolean enabled = mallAdForUpdateBo.getEnabled();
        String url = mallAdForUpdateBo.getUrl();
        Date addTime = mallAdForUpdateBo.getAddTime();
        Integer id = mallAdForUpdateBo.getId();
        Date updateTime = mallAdForUpdateBo.getUpdateTime();


        MallAd mallAd = new MallAd();


        mallAd.setName(name);
        mallAd.setContent(content);
        mallAd.setUrl(url);
        mallAd.setLink(link);
        mallAd.setPosition(position);
        mallAd.setEnabled(enabled);
        mallAd.setAddTime(new Date());
        mallAd.setUpdateTime(new Date());
        mallAd.setAddTime(addTime);
        mallAd.setId(id);
        mallAd.setUpdateTime(updateTime);

        mallAdMapper.insertSelective(mallAd);

        MallAdVo mallAdVo = new MallAdVo();

        mallAdVo.setId(mallAd.getId());
        mallAdVo.setUrl(mallAd.getUrl());
        mallAdVo.setAddTime(mallAd.getAddTime());
        mallAdVo.setContent(mallAd.getContent());
        mallAdVo.setEnabled(mallAd.getEnabled());
        mallAdVo.setLink(mallAd.getLink());
        mallAdVo.setName(mallAd.getName());
        mallAdVo.setPosition(mallAd.getPosition());
        mallAdVo.setUpdateTime(mallAd.getUpdateTime());

        return BaseRespVo.ok(mallAdVo);
    }

    /**
     * @param mallAd
     * @return 删除的业务逻辑并不是物理删除，只是将deleted属性设置为true，做到逻辑删除即可(本质是update)
     */
    @Override
    public int delete(MallAd mallAd) {

        mallAd.setDeleted(true);
        int rows = mallAdMapper.updateByPrimaryKeySelective(mallAd);

        return rows;
    }


}
